<template>
<div class="user">
        <!--搜索表单-->
        <div class="common-seach-wrap">
            <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item :label="$t('plus.activity_name')">
                    <el-input v-model="formInline.search" :placeholder="$t('plus.input_activity_name')"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="Search" @click="onSubmit">{{$t('plus.search')}}</el-button>
                    <el-button type="primary" @click="addClick" icon="Plus" v-auth="'/plus/package/add'">{{$t('plus.add_activity')}}</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="common-form">{{$t('plus.activity_list')}}</div>
        <div class="product-content point-list">
            <el-form ref="form" :model="form" label-width="100px">
                <div class="table-wrap">
                    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
                        <el-table-column prop="giftPackageId" label="ID"></el-table-column>
                        <el-table-column prop="name" :label="$t('plus.activity_name')" width="300"></el-table-column>
                        <el-table-column prop="startTime" :label="$t('plus.start_time')"></el-table-column>
                        <el-table-column prop="endTime" :label="$t('plus.end_time')"></el-table-column>
                        <el-table-column prop="totalNum" width="80" :label="$t('plus.issue_quantity')"></el-table-column>
                        <el-table-column width="80" :label="$t('plus.code_type')">
                          <template  #default="scope">
                            <label v-if="scope.row.codeType == 10">{{$t('plus.qr_type_batch')}}</label>
                            <label v-if="scope.row.codeType == 20">{{$t('plus.qr_type_unique')}}</label>
                          </template>
                        </el-table-column>
                        <el-table-column prop="people" width="80" :label="$t('plus.participant_count')"></el-table-column>
                        <el-table-column prop="stateText" width="80" :label="$t('plus.status')"></el-table-column>
                        <el-table-column fixed="right" width="200" :label="$t('plus.action')">
                            <template  #default="scope">
                                <el-button v-auth="'/plus/package/edit'" @click="editClick(scope.row.giftPackageId)"
                                           type="text" size="small"> {{$t('plus.edit')}} 

</el-button>
                                <el-button v-if="scope.row.status == 1" v-auth="'/plus/package/send'"
                                           @click="sendClick(scope.row.giftPackageId)"
                                           type="text" size="small"> {{$t('plus.publish')}} 
</el-button>
                                <el-button v-else="" v-auth="'/plus/package/send'"
                                           @click="endClick(scope.row.giftPackageId)" type="text"
                                           size="small"> {{$t('plus.terminate')}} 
</el-button>
                                <el-button v-auth="'/plus/package/orderlist'"
                                           @click="orderClick(scope.row.giftPackageId)" type="text"
                                           size="small"> {{$t('plus.purchase_record')}} 
</el-button>
                                <el-button v-auth="'/plus/package/qrcode'" @click="qrcodeClick(scope.row)"
                                           type="text"
                                           size="small"> {{$t('plus.promote_qrcode')}} 
</el-button>
                                <el-button v-auth="'/plus/package/delete'" @click="deleteClick(scope.row.giftPackageId)"
                                           type="text"
                                           size="small"> {{$t('plus.delete')}} 
</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </el-form>
            <!--分页-->
            <div class="pagination">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
                               :current-page="curPage"
                               :page-size="pageSize" layout="total, prev, pager, next, jumper" :total="totalDataNumber">
                </el-pagination>
            </div>
        </div>
        <!--会员等级-->
        <Qrcode v-if="open_qrcode" :open_qrcode="open_qrcode" :form="currentModel" @closeDialog="closeDialogFunc($event, 'qrcode')"></Qrcode>
    </div>
</template>

<script>
import PackageApi from '@/api/package.js';
import Qrcode from './dialog/Qrcode.vue';
export default {
  components: {
    Qrcode
  },
  data() {
    return {
      formInline: {
        search: ''
      },
      form: {},
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*是否加载完成*/
      loading: true,
      path: "",
      open_qrcode: false,
      currentModel: null
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = {};
      Params.search = self.formInline.search;
      Params.page = self.curPage;
      PackageApi.packageList(Params, true).then(data => {
        self.loading = false;
        self.tableData = data.data.records;
        self.totalDataNumber = data.data.total;
      }).catch(error => {
        self.loading = false;
      });
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      let self = this;
      self.curPage = 1;
      self.pageSize = val;
      self.getTableList();
    },
    /*添加*/
    addClick() {
      this.$router.push('/plus/package/add');
    },
    /*购买记录*/
    orderClick(e) {
      let self = this;
      this.$router.push({
        path: '/plus/package/orderlist',
        query: {
          giftPackageId: e
        }
      });
    },
    /*编辑*/
    editClick(e) {
      let self = this;
      this.$router.push({
        path: '/plus/package/edit',
        query: {
          giftPackageId: e
        }
      });
    },
    /* 查询*/
    onSubmit() {
      let self = this;
      let params = self.form;
      self.loading = true;
      self.getTableList();
    },
    /*删除*/
    deleteClick(e) {
      let self = this;
      ElMessageBox.confirm($t("plus.delete_confirm"), $t("plus.prompt"), {
        confirmButtonText: $t("plus.confirm_text"),
        cancelButtonText: $t("plus.cancel_text"),
        type: 'warning'
      }).then(() => {
        self.loading = true;
        PackageApi.delpackage({
          giftPackageId: e
        }, true).then(data => {
          self.loading = false;
          ElMessage({
            message: data.msg,
            type: 'success'
          });
          self.getTableList();
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {
        self.loading = false;
      });
    },
    /*发布*/
    sendClick(e) {
      let self = this;
      self.loading = true;
      PackageApi.send({
        giftPackageId: e
      }, true).then(data => {
        self.loading = false;
        ElMessage({
          message: data.msg,
          type: 'success'
        });
        self.getTableList();
      }).catch(error => {
        self.loading = false;
      });
    },
    /*终止*/
    endClick(e) {
      let self = this;
      self.loading = true;
      PackageApi.end({
        giftPackageId: e
      }, true).then(data => {
        self.loading = false;
        ElMessage({
          message: data.msg,
          type: 'success'
        });
        self.getTableList();
      }).catch(error => {
        self.loading = false;
      });
    },
    /*打开编辑*/
    qrcodeClick(e) {
      this.currentModel = e;
      this.open_qrcode = true;
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      this.open_qrcode = e.openDialog;
    }
  }
};
</script>

<style>
.point-list .el-input-number--mini {
        width: auto;
    }
</style>

